//
//  BHStatusCell.m
//  xinlangweibo0621
//
//  Created by boldseas on 2017/6/28.
//  Copyright © 2017年 renbohan. All rights reserved.
//

#import "BHStatusCell.h"
#import "BHStatus.h"
#import "BHUser.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>

#define margin 10


@interface BHStatusCell()

///** 原创微博整体 */
//@property (nonatomic, weak) UIView *originalView;
/** 头像 */
@property (nonatomic, strong) UIImageView *iconView;
/** 会员图标 */
@property (nonatomic, weak) UIImageView *vipView;
/** 配图 */
@property (nonatomic, weak) UIImageView *photoView;
/** 昵称 */
@property (nonatomic, strong) UILabel *nameLabel;
/** 时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 正文 */
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation BHStatusCell

/** 通过文字计算label的size */
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxW:(CGFloat)maxW
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = font;
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font
{
    return [self sizeWithText:text font:font maxW:MAXFLOAT];
}
/////



+ (instancetype)cellWithTableView:(UITableView *)tabbleView{

    static NSString *ID = @"status";
    BHStatusCell *cell = [tabbleView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[BHStatusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        /** 头像 */
        UIImageView *iconView = [[UIImageView alloc]init];
        [cell.contentView addSubview:iconView];
        cell.iconView = iconView;
        
        /** 昵称 */
        UILabel *nameLabel = [[UILabel alloc]init];
        [cell.contentView addSubview:nameLabel];
        cell.nameLabel = nameLabel;
        
        
        /** 正文 */
        UILabel *contentLabel = [[UILabel alloc]init];
        [cell.contentView addSubview:contentLabel];
        cell.contentLabel = contentLabel;
    }

    return cell;
}

- (void)setStatus:(BHStatus *)status{
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    _status = status;
    BHUser *user = self.status.user;
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
        make.size.mas_equalTo(self).priorityHigh();
    }];

    /** 头像 */
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default_small"]];
    self.iconView.contentMode = UIViewContentModeScaleAspectFit;
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.contentView).offset(margin);
        make.size.mas_equalTo(CGSizeMake(50, 50)).priorityHigh();
    }];


    /** 昵称 */
    self.nameLabel.text = user.name;
    self.nameLabel.numberOfLines = 1;
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(margin);
        make.left.mas_equalTo(self.iconView.mas_right).offset(margin);
    }];
    
    /** 正文 */
    self.contentLabel.text = self.status.text;
    self.contentLabel.font = [UIFont systemFontOfSize:13];
    self.contentLabel.numberOfLines = 0;
    
    CGFloat maxW = [UIScreen mainScreen].bounds.size.width - (2 * margin);
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    CGSize contentTextSize = [self.contentLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconView.mas_bottom).offset(margin);
        make.left.mas_equalTo(self.contentView).offset(margin);
        make.right.mas_equalTo(self.contentView).offset(-margin);
        make.bottom.mas_equalTo(self.contentView).offset(-margin);
        make.size.mas_equalTo(CGSizeMake(maxW, contentTextSize.height)).priorityHigh();
    }];
    
    [self layoutIfNeeded];
}

+ (CGFloat)heightTableCellWithModel:(BHStatus *)status{
    CGFloat cellHeight = margin + 50;
    
    NSString *contentText = status.text;
    
    CGFloat maxW = [UIScreen mainScreen].bounds.size.width - (2 * margin);
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
    
    CGSize contentTextSize = [contentText boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    
    CGFloat contentTextHeight = contentTextSize.height;
    
    cellHeight += (margin + contentTextHeight);
    
    return cellHeight;
}

@end










